<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 让对象支持for of
    const obj = {
      name: '小鹏',
      age: 18,
      index: 0,
      length: 2,
      [Symbol.iterator]() {
        return {
          index: this.index,
          length: this.length,
          next() {
            if (this.index === this.length) {
              return {
                value: undefined,
                done: true
              }
            } else {
              return {
                value: this.index++,
                done: false
              }
            }
          }
        }
      }
    }
    for (const item of obj) {
      console.log(item)
    }
  </script>
</body>

</html>